package de.tudarmstadt.ukp.muip.fibqdojo.util.japanese;

import com.google.common.base.Optional;

import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQToken;
import de.tudarmstadt.ukp.muip.fibqdojo.japanese.visitor.AbstractGetPiecesVisitor;
import de.tudarmstadt.ukp.muip.fibqdojo.japanese.visitor.GetFormPiecesVisitor;
import de.tudarmstadt.ukp.muip.fibqdojo.japanese.visitor.GetKanaPiecesVisitor;
import de.tudarmstadt.ukp.muip.fibqdojo.japanese.visitor.GetKeiPiecesVisitor;
import de.tudarmstadt.ukp.muip.fibqdojo.japanese.visitor.GetLemmaPiecesVisitor;
import de.tudarmstadt.ukp.muip.fibqdojo.japanese.visitor.GetDanPiecesVisitor;
import de.tudarmstadt.ukp.muip.fibqdojo.japanese.visitor.GetPosPiecesVisitor;

/**
 * Util class which provides frequently needed operations on Japanese tokens
 * 
 * @author Tobias Horsmann
 */
public class PiecesUtil {

    /**
     * Returns the kind of a verb (ichidan/godan) if it is a verb in the first
     * cell of the array.
     */
    public static String[] getDanPieces(IFIBQToken token) {
        return getPieces(token, new GetDanPiecesVisitor());
    }

    /**
     * Returns information about the grammatical form (kei) for the various
     * morphemes of this word.
     */
    public static String[] getKeiPieces(IFIBQToken token) {
        return getPieces(token, new GetKeiPiecesVisitor());
    }

    /**
     * Returns the kana writing in Katakana for the various morphemes of this
     * word
     */
    public static String[] getKanaPieces(IFIBQToken token) {
        return getPieces(token, new GetKanaPiecesVisitor());
    }

    /**
     * Returns the lemma for the various morphemes of this word
     */
    public static String[] getLemmaPieces(IFIBQToken token) {
        return getPieces(token, new GetLemmaPiecesVisitor());
    }

    /**
     * Returns the part of speech tags for the various morphemes of this word
     */
    public static String[] getPosPieces(IFIBQToken token) {
        return getPieces(token, new GetPosPiecesVisitor());
    }

    public static String[] getFormPieces(IFIBQToken token) {
        return getPieces(token, new GetFormPiecesVisitor());
    }

    /**
     * Simply tests whether the index is still within array boundaries.
     */
    public static boolean isWithinArrayBoundaries(String[] array, int i) {
        if (i < array.length) {
            return true;
        }
        return false;
    }

    private static String[] getPieces(IFIBQToken token, AbstractGetPiecesVisitor visitor) {
        token.accept(visitor);
        Optional<String[]> pieces = visitor.getPieces();
        if (pieces.isPresent()) {
            return pieces.get();
        }
        return new String[] {};

    }

}
